/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ModelPane.java
 *
 * Created on 18.3.2011, 12:56:49
 */
package cz.muni.fi.rum.gui.component;

import cz.muni.fi.rum.domainmodel.Reflector;
import cz.muni.fi.rum.domainmodel.ReflectorListener;
import java.awt.datatransfer.Transferable;
import javax.swing.DropMode;
import javax.swing.JComponent;
import javax.swing.TransferHandler;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.jdesktop.application.Action;
import org.noos.xing.mydoggy.Content;
import org.noos.xing.mydoggy.MultiSplitConstraint;
import org.noos.xing.mydoggy.ToolWindowManager;

/**
 *
 * @author pmikulasek
 */
public class ModelPane extends javax.swing.JPanel {

    private Reflector reflector;
    private static ModelPane instance;
    private TreeModel model;
    private ToolWindowManager wm;

    /** Creates new form ModelPane */
    private ModelPane() {
        model = new TreeModel();
        initComponents();
        modelTree.setDragEnabled(true);
        modelTree.setDropMode(DropMode.ON_OR_INSERT);
        modelTree.setTransferHandler(new TreeTransferHandler());
    }

    public static ModelPane getInstance() {
        if (instance == null) {
            instance = new ModelPane();
        }
        return instance;
    }

    public void setWindowManager(ToolWindowManager wm) {
        this.wm = wm;
    }

    public void setReflector(Reflector reflector) {
        this.reflector = reflector;
        this.model.setReflector(reflector);
    }

    public Reflector getPresentReflector() {
        return reflector;
    }

    @Action
    public void addNewListenerAction() {
        ListenerNode list = model.createNewReflectorListener();
        modelTree.clearSelection();
        modelTree.addSelectionPath(new TreePath(list.getPath()));//scrollPathToVisible();
        //scrollPathToVisible();
        registerListenerContent(list, (ReflectorListener) list.getContent());
    }

    @Action
    public void controlActualModel() {
        model.actualize();
    }

    private Content registerListenerContent(ListenerNode list, ReflectorListener reflectorListener) {
        Content content = wm.getContentManager().addContent(
                list.getId(),
                list.toString(),
                null,
                new ListenerCreatorPane(list, reflectorListener, wm),
                "Listener tool tip",
                new MultiSplitConstraint(wm.getContentManager().getContent(0), 0));
        return content;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        modelTree = new javax.swing.JTree(model);
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        modelTree.setName("modelTree"); // NOI18N
        modelTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                modelTreeMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(modelTree);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getActionMap(ModelPane.class, this);
        jButton1.setAction(actionMap.get("addNewListenerAction")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getResourceMap(ModelPane.class);
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setMaximumSize(new java.awt.Dimension(23, 23));
        jButton1.setMinimumSize(new java.awt.Dimension(23, 23));
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setPreferredSize(new java.awt.Dimension(23, 23));
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        jButton3.setAction(actionMap.get("controlActualModel")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addGap(4, 4, 4)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void modelTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_modelTreeMouseClicked
        int selRow = modelTree.getRowForLocation(evt.getX(), evt.getY());
        TreePath selPath = modelTree.getPathForRow(selRow);
        if (selRow != -1) {
            if (evt.getClickCount() == 1) {
//                System.out.println("single " + selPath +selRow);
            }
        }
        if (evt.getClickCount() == 2) {
            if (selPath != null) {
                Object component = selPath.getLastPathComponent();
                if (component instanceof ListenerNode) {
                    ListenerNode list = (ListenerNode) component;
                    Object reflectorListener = list.getContent();
                    if (reflectorListener instanceof ReflectorListener) {
                        Content content = wm.getContentManager().getContent(list.getId());
                        //v pripade ze neni otevreny dany content tak ho registrovat
                        if (content == null) {
                            content = registerListenerContent(list, (ReflectorListener) reflectorListener);
                        }
                        content.setEnabled(true);
                        content.setSelected(true);
                    }
//                System.out.println("double " + selPath + selRow);
                }
            }
        }


    }//GEN-LAST:event_modelTreeMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTree modelTree;
    // End of variables declaration//GEN-END:variables
}
